const initialState = {
  commentInfo: {} as CommentInfoType,
  originComment: {} as CommentType,
  replys: {} as CommentInfoType,
};

export default function CommentReducer(
  state = initialState,
  action: CommentActionType
): typeof initialState {
  switch (action.type) {
    case 'comment/SaveCommentInfo':
      return {
        ...state,
        commentInfo: action.payload,
      };
    case 'comment/ClearCommentInfo':
      return {
        ...state,
        commentInfo: {} as CommentInfoType,
      };
    case 'comment/SaveOriginComment':
      return {
        ...state,
        originComment: action.payload,
      };
    case 'comment/ClearOriginComment':
      return {
        ...state,
        originComment: {} as CommentType,
      };
    case 'comment/SaveReplys':
      return {
        ...state,
        replys: action.payload,
      };
    case 'comment/ClearReplys':
      return {
        ...state,
        replys: {} as any,
      };
    default:
      return state;
  }
}

export type CommentActionType =
  | {
      type: 'comment/SaveCommentInfo';
      payload: CommentInfoType;
    }
  | {
      type: 'comment/ClearCommentInfo';
    }
  | {
      type: 'comment/SaveOriginComment';
      payload: CommentType;
    }
  | {
      type: 'comment/ClearOriginComment';
    }
  | {
      type: 'comment/SaveReplys';
      payload: CommentInfoType;
    }
  | {
      type: 'comment/ClearReplys';
    };

export type CommentInfoType = {
  total_count: number;
  end_id: string;
  last_id: string;
  results: CommentType[];
};

export type CommentType = {
  com_id: string;
  content: string;
  reply_count: number;
  pubdate: string;
  is_followed: boolean;
  is_liking: boolean;
  like_count: number;
  aut_id: string;
  aut_name: string;
  aut_photo: string;
};
